package com.seanliao.nav.common.enums;

import cn.hutool.core.util.EnumUtil;

/**
 * 返回结果枚举
 *
 * @author Sean_Liao
 */
public enum ResultCodeEnum {
    // 成功
    SUCCESS(0, "success"),
    // 错误
    FAIL(-1, "error"),

    BUSINESS_ERR(10001, "事务异常"),
    SERVER_ERR(10002, "服务器异常"),
    PARAM_ERR(10003, "参数异常"),
    TOKEN_EXPIRE(10004, "令牌过期"),
    TOKEN_ERR(10005, "令牌异常"),
    SESSION_EXPIRATION(20001, "会话过期,请重启小程序"),
    // 末位占位
    DEFAULT(65535, "占位");

    private final int code;

    private final String message;

    ResultCodeEnum(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据状态码获取枚举体
     */
    public static ResultCodeEnum getByCode(int code) {
        return EnumUtil.getBy(ResultCodeEnum::getCode, code);
    }

    /**
     * 根据状态码获取提示信息
     */
    public static String getMsgByCode(int code) {
        ResultCodeEnum codeEnum = getByCode(code);
        return codeEnum != null ? codeEnum.getMessage() : "";
    }
}
